package com.qidian.by.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.by.cms.service.IContentCategoryService;
import com.qidian.by.pojo.ContentCategory;
import com.qidian.by.util.exceptions.ServiceExceptionEnum;
import com.qidian.by.util.pojo.EsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 内容分类 前端控制器
 * </p>
 *
 * @author 小可怎么都睡不够
 * @since 2024-05-06
 */
@RestController
@RequestMapping(value = "/contentCategory",produces = "application/json;charset=utf-8")
public class ContentCategoryController {

    @Autowired
    private IContentCategoryService contentCategoryService;
    @PutMapping("/save")
    public EsResult save(@RequestBody ContentCategory contentCategory) {
        return contentCategoryService.saveCategory(contentCategory);
    }

    @GetMapping("/queryByParentId")
    public EsResult get(@RequestParam Long parentId) {
        QueryWrapper<ContentCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<ContentCategory> data = contentCategoryService.list(queryWrapper);
        return EsResult.ok("查询成功",data);
    }

    @GetMapping("/queryByCatId")
    public EsResult getById(@RequestParam Long catId){
        QueryWrapper<ContentCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cat_id",catId);
        return contentCategoryService.getOne(queryWrapper) == null ? EsResult.error(ServiceExceptionEnum.DEFAULT_FAIL) : EsResult.ok("查询成功",contentCategoryService.getOne(queryWrapper));
    }
//    编辑类别信息
    @PostMapping("/update")
    public EsResult update(@RequestBody ContentCategory contentCategory) {
        if (contentCategory.getCatId() == null) {
            return EsResult.error(ServiceExceptionEnum.PARAM_ERROR);
        }
        return contentCategoryService.updateById(contentCategory) ? EsResult.ok() : EsResult.error(ServiceExceptionEnum.DEFAULT_FAIL);
    }

//    删除类别信息
    @DeleteMapping("/delete")
    public EsResult delete(@RequestParam Long catId) {
        return contentCategoryService.removeById(catId) ? EsResult.ok() : EsResult.error(ServiceExceptionEnum.DEFAULT_FAIL);
    }

    @GetMapping("/queryByName")
    public EsResult getCategoryByName(@RequestParam String name) {
        return EsResult.ok("查询成功", contentCategoryService.queryByName(name));
    }

    @GetMapping("/queryByNameWithNoParent")
    public EsResult getCategoryByNameWithNoParent(@RequestParam String name) {
        QueryWrapper<ContentCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("cat_name",name);
        return EsResult.ok("查询成功", contentCategoryService.list(queryWrapper));
    }
    @GetMapping("/queryAll")
    public EsResult queryAll() {
        return contentCategoryService.queryAll();
    }
}
